package org.fh.controller.fhoa;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.act.AcStartController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.ClockinmyService;
import org.fh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 打卡信息
 */

@Controller
@RequestMapping("/clockinmy")
public class ClockinmyController extends AcStartController {

    @Autowired
    private ClockinmyService clockinmyService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 保存
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
//	@RequiresPermissions("clockinmy:add")
    @ResponseBody
    public Object add(
            @RequestParam(value = "FIMGA", required = false) MultipartFile filea,
            @RequestParam(value = "LOANTITLE", required = false) String LOANTITLE,
            @RequestParam(value = "CUSTOMER_NAME", required = false) String CUSTOMER_NAME,
            @RequestParam(value = "CUREECONTENT", required = false) String CUREECONTENT,
            @RequestParam(value = "COMPANY", required = false) String COMPANY,
            @RequestParam(value = "LONGITUDE", required = false) String LONGITUDE,
            @RequestParam(value = "LATITUDE", required = false) String LATITUDE
//			@RequestParam(value="PHOTOADDA",required=false) String PHOTOADDA
    ) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd.put("CLOCK_ID", this.get32UUID());                    //主键
        pd.put("LOANTITLE", LOANTITLE);
        pd.put("COMPANY", COMPANY);
        pd.put("CUSTOMER_NAME", CUSTOMER_NAME);
        pd.put("LONGITUDE", LONGITUDE);
        pd.put("CUREECONTENT", CUREECONTENT);
        pd.put("LATITUDE", LATITUDE);

        String ffile = DateUtil.getDays(), fileNamea = "", fileNameb = "", fileNamec = "", fileNamed = "", fileNamee = "", fileNamef = "", fileNameg = "", fileNameh = "", fileNamei = "", fileNamej = "", fileNamek = "", fileNamel = "", fileNamem = "", fileNamen = "", fileNameo = "", fileNamep = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;

        if (null != filea && !filea.isEmpty()) {
            fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDA", readFilePath + "/" + fileNamea);
        }

//		写入表
        pd.put("CTIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("USERNAME", Jurisdiction.getUsername());
        pd.put("UNAME", Jurisdiction.getName());    //
        pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
//			pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        clockinmyService.save(pd);
//		try {
//			/** 工作流的操作 **/
//			Map<String,Object> mapa = new LinkedHashMap<String, Object>();
//			mapa.put("申请人员", Jurisdiction.getName());			//当前用户的姓名
//			mapa.put("打卡地点", LOANTITLE);
//			mapa.put("打卡时间", DateUtil.date2Str(new Date()));
//			String USERNAME = Jurisdiction.getUsername();
//			mapa.put("USERNAME", USERNAME);						//指派代理人为当前用户
//			startProcessInstanceByKeyHasVariables("KEY_leave",mapa,USERNAME);//启动流程实例(请假单流程)通过KEY
//													//记录存入数据库
//			map.put("ASSIGNEE_",Jurisdiction.getUsername());				//用于给待办人发送新任务消息
//		} catch (Exception e) {
//			errInfo = "errer";
//		}
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
//	@RequiresPermissions("clockinmy:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        clockinmyService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
//	@RequiresPermissions("clockinmy:edit")
    @ResponseBody
    public Object edit(
            @RequestParam(value = "PHOTOADDA", required = false) MultipartFile filea,
            @RequestParam(value = "LOANTITLE", required = false) String LOANTITLE,
            @RequestParam(value = "CUREECONTENT", required = false) String CUREECONTENT,
            @RequestParam(value = "COMPANY", required = false) String COMPANY,
            @RequestParam(value = "LONGITUDE", required = false) String LONGITUDE,
            @RequestParam(value = "LATITUDE", required = false) String LATITUDE,
            @RequestParam(value = "CUSTOMER_NAME", required = false) String CUSTOMER_NAME,
            @RequestParam(value = "CLOCK_ID", required = false) String CLOCK_ID

    ) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        String ffile = DateUtil.getDays(), fileNamea = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;


        if (null != filea && !filea.isEmpty()) {
            fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());                //执行上传
            pd.put("PHOTOADDA", readFilePath + "/" + fileNamea);
        }
        pd.put("LOANTITLE", LOANTITLE);
        pd.put("COMPANY", COMPANY);
        pd.put("CUREECONTENT", CUREECONTENT);
        pd.put("LONGITUDE", LONGITUDE);
        pd.put("LATITUDE", LATITUDE);
        pd.put("CUSTOMER_NAME", CUSTOMER_NAME);
        pd.put("CTIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("USERNAME", Jurisdiction.getUsername());    //
        pd.put("CLOCK_ID", CLOCK_ID);                    //主键
        pd.put("UNAME", Jurisdiction.getName());
        pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        clockinmyService.edit(pd);

        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 去修改页面
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = clockinmyService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("clockinmy:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String USERNAME = Jurisdiction.getUsername();                    //用户名
        pd.put("USERNAME", USERNAME);
        page.setPd(pd);
        List<PageData> varList = clockinmyService.list(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 导出到excel
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("打卡地点");    //1
        titles.add("公司/单位");    //1
        titles.add("经度");    //2
        titles.add("纬度");    //3
        titles.add("图片地址");    //4
        titles.add("姓名");    //5
        titles.add("客户姓名");    //5
        titles.add("创建时间");    //6
        dataMap.put("titles", titles);
        List<PageData> varOList = clockinmyService.listAll(pd);
        List<PageData> varList = new ArrayList<PageData>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("LOANTITLE"));        //1
            vpd.put("var2", varOList.get(i).getString("COMPANY"));        //1

            vpd.put("var3", varOList.get(i).getString("LONGITUDE"));        //2
            vpd.put("var4", varOList.get(i).getString("LATITUDE"));        //3
            vpd.put("var5", varOList.get(i).getString("PHOTOADDA"));        //4
            vpd.put("var6", varOList.get(i).getString("USERNAME"));        //5
            vpd.put("var7", varOList.get(i).getString("CUSTOMER_NAME"));        //6
            vpd.put("var8", varOList.get(i).getString("CTIME"));        //6

            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }


//	/**
//	 * 查询打卡统计
//	 *
//	 * @return
//	 */
//	@RequestMapping(value = "/clockDetail")
//	@ResponseBody
//	public Object selectNewLogDetail(){
////		(@RequestParam(value="USERNAME",required=false) String USERNAME)
//		Map<String, Object> map = new HashMap<>();
//		String errInfo = "success";
//		String USERNAME = Jurisdiction.getUsername();					//用户名
//		System.out.println("USERNAME---------------------"+USERNAME);
//		List<ClockinmyDetailDTO> clockDetail = clockinmyService.getClockDetail(USERNAME);
//
//		System.out.println("clockDetail-------------------"+clockDetail);
//		map.put("resultList", clockDetail);
//		map.put("result", errInfo);
//		return map;
//	}
}
